<?php
/*  vim: set expandtab shiftwidth=4 softtabstop=4 tabstop=4:    */

class Auth {
    public static $auth;

    public function __construct($driver) {
        try {
            if(self::checkStorageDriver($driver) === FALSE) {
                throw new Exception("Invalid authorization storage driver");
            }
        } catch (Exception $e) {
            print "Failed to construct Auth class [". $e->getMessage() ."]";
        }
    }

    public function checkStorageDriver($driver) {
        switch($driver) {
            case "DB":
                try {
                    if(!isset(DB::$db) || !is_object(DB::$db)) {
                        throw new Exception("No database initialized");
                    } else {
                        self::$auth =   new Auth_DB();
                    }
                } catch (Exception $e) {
                    print "Auth - Failed to set storage driver [". $e->getMessage() ."]";
                }

                return TRUE;
            break;

            case "LDAP":
                $config     =   Control::getConfigs();

                if(isset($config['auth']['ldap']['hostname']) && !empty($config['auth']['ldap']['hostname'])) {
                    self::$auth     =   new Auth_LDAP($config['auth']['ldap']['hostname']);
                } else {
                    throw new Exception("Failed to set authorization engine");
                }
            break;

            default:
                return FALSE;
        }
    }

}
